package com.fypg.module.medical.controller.admin.smslog;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.fypg.framework.common.pojo.PageParam;
import com.fypg.framework.common.pojo.PageResult;
import com.fypg.framework.common.pojo.CommonResult;
import com.fypg.framework.common.util.object.BeanUtils;
import static com.fypg.framework.common.pojo.CommonResult.success;

import com.fypg.framework.excel.core.util.ExcelUtils;

import com.fypg.framework.apilog.core.annotation.ApiAccessLog;
import static com.fypg.framework.apilog.core.enums.OperateTypeEnum.*;

import com.fypg.module.medical.controller.admin.smslog.vo.*;
import com.fypg.module.medical.dal.dataobject.smslog.MedicalSmslogDO;
import com.fypg.module.medical.service.smslog.SmslogService;

@Tag(name = "管理后台 - 短信发送记录")
@RestController
@RequestMapping("/medical/smslog")
@Validated
public class SmslogController {

    @Resource
    private SmslogService smslogService;

    @PostMapping("/create")
    @Operation(summary = "创建短信发送记录")
    @PreAuthorize("@ss.hasPermission('medical:smslog:create')")
    public CommonResult<String> createSmslog(@Valid @RequestBody SmslogSaveReqVO createReqVO) {
        return success(smslogService.createSmslog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新短信发送记录")
    @PreAuthorize("@ss.hasPermission('medical:smslog:update')")
    public CommonResult<Boolean> updateSmslog(@Valid @RequestBody SmslogSaveReqVO updateReqVO) {
        smslogService.updateSmslog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除短信发送记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('medical:smslog:delete')")
    public CommonResult<Boolean> deleteSmslog(@RequestParam("id") String id) {
        smslogService.deleteSmslog(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除短信发送记录")
                @PreAuthorize("@ss.hasPermission('medical:smslog:delete')")
    public CommonResult<Boolean> deleteSmslogList(@RequestParam("ids") List<String> ids) {
        smslogService.deleteSmslogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得短信发送记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('medical:smslog:query')")
    public CommonResult<SmslogRespVO> getSmslog(@RequestParam("id") String id) {
        MedicalSmslogDO smslog = smslogService.getSmslog(id);
        return success(BeanUtils.toBean(smslog, SmslogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得短信发送记录分页")
    @PreAuthorize("@ss.hasPermission('medical:smslog:query')")
    public CommonResult<PageResult<SmslogRespVO>> getSmslogPage(@Valid SmslogPageReqVO pageReqVO) {
        PageResult<MedicalSmslogDO> pageResult = smslogService.getSmslogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SmslogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出短信发送记录 Excel")
    @PreAuthorize("@ss.hasPermission('medical:smslog:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSmslogExcel(@Valid SmslogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MedicalSmslogDO> list = smslogService.getSmslogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "短信发送记录.xls", "数据", SmslogRespVO.class,
                        BeanUtils.toBean(list, SmslogRespVO.class));
    }

}
